package com.vilen.learn.java7.V3_1;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by vilen on 2017/9/23.
 *
 * 代码中注入的是一个引用凭据（agentFinderType），而不是真正实现AgentFinder的对象。
    方法getFriendlyAgents中还有获取其依赖项的代码，达不到只关注自身职能的理想状态。
 */
public class HollywoodServiceWithFactory {
    // 注入 agentFinderType
    public List<Agent> getFriendlyAgents(String agentFinderType){
        // 通过工厂实例获取AgentFinder
        AgentFinderFactory factory = AgentFinderFactory.getInstance();
        AgentFinder finder = factory.getAgentFinder(agentFinderType);
        List<Agent> agents = finder.findAllAgents();
        List<Agent> friendlyAgents  = FilterAgents(agents,"Java Developers");
        return friendlyAgents;
    }

    private static List<Agent> FilterAgents(List<Agent> agents, String agentType) {
        List<Agent> filteredAgents = new ArrayList<>();
        for (Agent agent : agents) {
            if (agent.getType().equals("Java Developers")) {
                filteredAgents.add(agent);
            }
        }
        return filteredAgents;
    }
}
